# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=mako

pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.9.1
pkgrel=2
pkgdesc="A super-fast templating language that borrows the best ideas from the existing templating languages (mingw-w64)"
arch=('any')
groups=("${MINGW_PACKAGE_PREFIX}")
license=('custom')
url="https://pypi.python.org/pypi/Mako"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools" "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-nose" "${MINGW_PACKAGE_PREFIX}-python3-nose")
options=('staticlibs')
source=("https://pypi.python.org/packages/source/M/Mako/Mako-0.9.1.tar.gz")
md5sums=('fe3f394ef714776d09ec6133923736a7')

prepare() {
  cd ${srcdir}
  cp -r Mako-$pkgver python2-build
  cp -r Mako-$pkgver python3-build
    mv python3-build/scripts/mako-render python3-build/scripts/python3-mako-render
    ${MINGW_PREFIX}/bin/2to3 -nw python3-build/scripts/python3-mako-render
    sed -i 's/mako-render/python3-mako-render/' python3-build/setup.py
}

build() {
  cd "$srcdir"
  for builddir in python{2,3}-build; do
    pushd $builddir
    ${MINGW_PREFIX}/bin/${builddir%-build} setup.py build
    popd
  done
}

check() {
  cd "$srcdir"
  for builddir in python{2,3}-build; do
    pushd $builddir
    ${MINGW_PREFIX}/bin/${builddir%-build} setup.py test
    popd
  done
}

package_python2-mako() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-markupsafe" "${MINGW_PACKAGE_PREFIX}-python2-beaker")

  cd "$srcdir/python2-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="$pkgdir" -O1

  # fix python command in files
  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done

  install -Dm644 LICENSE "${pkgdi}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"
}

package_python3-mako() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-markupsafe" "${MINGW_PACKAGE_PREFIX}-python3-beaker")

  cd "$srcdir/python3-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="$pkgdir" -O1

  # fix python command in files
  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done
  
  install -Dm644 LICENSE "${pkgdi}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"
}

package_mingw-w64-i686-python2-mako() {
  package_python2-mako
}

package_mingw-w64-i686-python3-mako() {
  package_python3-mako
}

package_mingw-w64-x86_64-python2-mako() {
  package_python2-mako
}

package_mingw-w64-x86_64-python3-mako() {
  package_python3-mako
}
